package com.dqhl.business.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.dqhl.business.Base.BaseFragment;
import com.dqhl.business.R;
import com.dqhl.business.adapter.GoodsManagerFlagAdapter;
import com.dqhl.business.modle.GoodsManager_flag;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * description: 已禁用
 * autour: guanluocang
 * date: 2016/11/9 16:21
 */
public class GoodsManagerCloseFragment extends BaseFragment implements View.OnClickListener {

    private ListView lv_goodsManagerClose;
    private List<GoodsManager_flag> goodsManagerFlagList;
    private GoodsManagerFlagAdapter flagAdapter;
    private TextView tv_no_data;
    private String type_id;
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    Handler handler = new Handler();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_manager_close, container, false);
        initView(view);
//        initData();
        pullRefresh();
        return view;
    }

    private void initView(View view) {
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data);
        lv_goodsManagerClose = (ListView) view.findViewById(R.id.lv_goodsManagerClose);
        (ptrClassicFrameLayout) = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_list);
    }

    private void pullRefresh() {
        //下拉刷新
        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);

        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrClassicFrameLayout.refreshComplete();
                        //获取数据
                        initData();
                    }
                }, 1000);
            }
        });

        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ptrClassicFrameLayout.loadMoreComplete(true);

                    }
                }, 1000);
            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    public void initData() {
        type_id = getActivity().getIntent().getStringExtra("id");
        Dlog.e("你选择的分类ID" + type_id);
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.management_goods_type_goods_list);
        params.addBodyParameter("seller_id", user.getUser_id());
        params.addBodyParameter("class_id", type_id);
        // 1  开启  2  关闭
        params.addBodyParameter("open", 2 + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("GGClose" + result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getErrMsg(result);
                String data = JsonUtils.getData(result);
                if (0 == errCode) {
                    tv_no_data.setVisibility(View.GONE);
                    lv_goodsManagerClose.setVisibility(View.VISIBLE);

                    goodsManagerFlagList = JSON.parseArray(data, GoodsManager_flag.class);
                    flagAdapter = new GoodsManagerFlagAdapter(getActivity(), goodsManagerFlagList,
                            new GoodsManagerFlagAdapter.OnOpenCloseListener() {
                                @Override
                                public void onOpenOrClose(int position) {
                                    updataStatus(goodsManagerFlagList.get(position).getId());
                                }
                            });
                    lv_goodsManagerClose.setAdapter(flagAdapter);
                    lv_goodsManagerClose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                            Bundle bundle = new Bundle();
//                            bundle.putString("id", goodsManagerFlagList.get(i).getId());
//                            overlay(GoodsDetailPreviewActivity.class, bundle);
                        }
                    });
                } else {
                    toast("暂无数据");
                    tv_no_data.setVisibility(View.VISIBLE);
                    lv_goodsManagerClose.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }

    //修改状态
    public void updataStatus(String position) {
        showCustomLoadBar(".");
        RequestParams params = new RequestParams(Config.management_goods_moify_status);
        Dlog.e("GG启用传值" + position);
        params.addBodyParameter("id", position);
        params.addBodyParameter("open", "1");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("GG启用" + result);
                int errCode = JsonUtils.getErrCode(result);
                if (errCode == 0) {
                    pullRefresh();
                } else {
                    toast("修改失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }
}
